﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Objects;
using System.Web.Services;
using System.Web.Script.Services;
namespace AbingtonBooks
{
    public partial class Master : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Customer"] != null) {
                Customer thisCustomer = (Customer)Session["Customer"];
                txtWelcome.Text = "Welcome " + thisCustomer.FirstName;
                DateTime lastLogin = (thisCustomer.LastLogon != null) ? (DateTime)thisCustomer.LastLogon : DateTime.Now;
                txtLastLogin.Text = "Last Login: " + lastLogin.ToString("MM/dd/yyyy");
                LoggedIn.Visible = true;

            }
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            Customer thisCustomer = (Customer)Session["Customer"];
            using (dbAbingtonBooks context = new dbAbingtonBooks()) {
                context.Customers.Attach(thisCustomer);
                context.Entry(thisCustomer).Property(x => x.LastLogon).CurrentValue = DateTime.Now;
                context.SaveChanges();
                LoggedIn.Visible = false;
            }
            Session.Clear();
            Response.Redirect("~/Default.aspx");
        }
    }
}